﻿using System;
using System.Collections.Generic;
using System.Data;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.company
{
    public partial class CompanyCardType : BaseAdmin
    {
        readonly DALProfessionAreaType theProfessionType = new DALProfessionAreaType();
        readonly DALCardType theCardType = new DALCardType();
        readonly DALCompanyCardType theCompanyCardType = new DALCompanyCardType();
        tbCompanyCardType oCompanyType = new tbCompanyCardType();
        readonly adminlogin theAdminLogin = new adminlogin();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.CompanyList);
                this.BuildRuleList();
            }
        }

        private void BuildRuleList()
        {
            StringBuilder strHead = new StringBuilder();
            StringBuilder strBody = new StringBuilder();
            StringBuilder strTable = new StringBuilder();
            StringBuilder strCheckedAdmin = new StringBuilder(",");
            DataSet ds = new DataSet();
            DataSet dsSub = new DataSet();
            string sCompanyID = Request.QueryString["id"];

            dalCompany theCompany = new dalCompany();

            ds = theProfessionType.GetList();
            dsSub = theCardType.GetList();
            strTable.Append("<table class='tableRule'  border=\"1\" cellpadding=\"3\" cellspacing=\"1\" bordercolor=\"#E2E7EB\">");
            if (ds.Tables.Count > 0)
            {
                 strHead.Append("<tr class='ruleHead'*>");
                 strBody.Append("<tr>");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    strHead.Append("<td width='120px' nowrap=\"nowrap\">" + dr["Name"].ToString() + "</td>");
                    strBody.Append("<td valign='top' nowrap=\"nowrap\">");
                    if (dsSub.Tables.Count > 0)
                    {
                        dsSub.Tables[0].DefaultView.RowFilter = " ProfessionAreaTypeId = " + dr["Id"].ToString();
                     
                        for (int i = 0; i < dsSub.Tables[0].DefaultView.Count; i++)
                        {
                            string sChecked = "";
                            string sOnSale = "1";
                            if ( theCompanyCardType.GetRule(int.Parse(sCompanyID),int.Parse(dsSub.Tables[0].DefaultView[i]["Id"].ToString())))
                            {
                                sChecked = "checked='checked'";
                                oCompanyType = theCompanyCardType.GetModel(int.Parse(sCompanyID), int.Parse(dsSub.Tables[0].DefaultView[i]["Id"].ToString()));
                                if (oCompanyType != null) sOnSale = oCompanyType.OnSale.ToString();
                                strCheckedAdmin.Append(dsSub.Tables[0].DefaultView[i]["Id"] + ",");
                            }
                            strBody.Append("<br/><input type='checkbox' onclick='setAdminId(this.id)' " + sChecked +" id='" + dsSub.Tables[0].DefaultView[i]["Id"].ToString() + "'/>" 
                                +" <input type='text' size='1' value='" + sOnSale + "'  name='txt" +dsSub.Tables[0].DefaultView[i]["Id"].ToString() +"' />"
                                + dsSub.Tables[0].DefaultView[i]["Name"].ToString() );
                        }
                    }
                    strBody.Append("</td>");
                }
                strBody.Append("</tr>");
                strHead.Append("</tr>");
            }
            strTable.Append(strHead.ToString());
            strTable.Append(strBody.ToString());
            strTable.Append("</table>");

            this.ltRuleList.Text = strTable.ToString();
            this.hdList.Value = strCheckedAdmin.ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string sRuleIds = string.Empty;
            if (this.hdList.Value.Length > 1)
            {
               sRuleIds =  this.hdList.Value.Substring(1, this.hdList.Value.Length - 2);
            }
            else
            {
                sRuleIds = "0,0";
            }

            int iCompanyId = int.Parse(Request.QueryString["id"]);

            theCompanyCardType.Save(iCompanyId, sRuleIds);

            string[] sArryIds = sRuleIds.Split(',');

            foreach (string sId in sArryIds)
            {
                try
                {
                    if (sId != "0")
                    {
                        decimal dValue = decimal.Parse(Request.Form["txt" + sId]);
                        if (dValue <= 0 || dValue > 1)
                        {
                            Response.Write("<script>alert('请检查折扣，折扣在大于0小于1之间的数字！');</script>");

                            return;
                        }
                    }
                }
                catch
                {
                    Response.Write("<script>alert('请检查折扣，折扣在大于0小于1之间的数字！');</script>");
                    return;
                }
            }      


        

            foreach (string sId in sArryIds)
            {
                oCompanyType = theCompanyCardType.GetModel(iCompanyId, int.Parse(sId));
                if (oCompanyType != null)
                {
                    oCompanyType.OnSale = decimal.Parse(Request.Form["txt" + sId]);
                    theCompanyCardType.Update(oCompanyType);
                }
            }


            Response.Write("<script>alert('保存成功！');window.location.href='CompanyCardType.aspx?id=" + Request.QueryString["id"] + "';</script>");
        }
    }
}